﻿function validateForm() {
    var _status = true;
    var _message = "";

    if (document.getElementById("ordernumber").value.length != 20) {
        _message += "Invalid order number.\n";
        _status = false;
    }

    if (document.getElementById("amount").value.length == 0) {
        _message += "Invalid amount.\n";
        _status = false;
    }
  

    if (document.getElementById("email").value.length == 0) {
        _message += "Enter email address.\n";
        _status = false;
    }
    else if (!checkEmail()) {
        _message += "Enter valid email address.\n";
        _status = false;
    }

    if (document.getElementById("ccname").value.length == 0) {
        _message += "Enter name on credit card.\n";
        _status = false;
    }

    if (document.getElementById("txtCardNo1").value.length == 4 &&
        document.getElementById("txtCardNo2").value.length == 4 &&
        document.getElementById("txtCardNo3").value.length == 4 &&
        document.getElementById("txtCardNo4").value.length == 4) {
        document.getElementById("ccnumber").value = document.getElementById("txtCardNo1").value.toString();
        document.getElementById("ccnumber").value += document.getElementById("txtCardNo2").value.toString();
        document.getElementById("ccnumber").value += document.getElementById("txtCardNo3").value.toString();
        document.getElementById("ccnumber").value += document.getElementById("txtCardNo4").value.toString();

        if (document.getElementById("ccnumber").value.length < 13) {
            _message += "Enter credit card number completely.\n";
            _status = false;
        }
        else {
            if (document.getElementById("rdoVisa").checked) {
                if (!checkCreditCard(document.getElementById("ccnumber").value, "Visa")) {
                    _message += ccErrors[ccErrorNo] + "\n";
                    _status = false;
                }
            }
            else if (document.getElementById("rdoMaster").checked) {
                if (!checkCreditCard(document.getElementById("ccnumber").value, "MasterCard")) {
                    _message += ccErrors[ccErrorNo] + "\n";
                    _status = false;
                }
            }
            else {
                _message += "Select card type.\n";
                _status = false;
            }
        }
    }
    else {
        _message += "Enter credit card number completely.\n";
        _status = false;
    }

    if (document.getElementById("cvv2").value.length = 0) {
        _message += "Enter CVV No.\n";
        _status = false;
    }
    else if (document.getElementById("cvv2").value.length < 3) {
        _message += "Enter 3 digits CVV No.\n";
        _status = false;
    }

    if (document.getElementById("ccdate").value.length < 4) {
        _message += "Select card expiry date.";
        _status = false;
    }
    else if (!CheckCCDate()) {
        _message += "Card expiry date is invalid.";
        _status = false;
    }

    if (_status == false) {
        alert(_message);
    }
    else {
        if (confirm("Please confirm to proceed.\nPress OK to continue, or Cancel to stay on the current page.")) {
            window.onbeforeunload = null;
        }
        else {
            _status = false;
        }
    }

    return _status;
}

function checkEmail() {
    var _status = true;
    var email = document.getElementById("email");
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {

        _status = false;
    }
    return _status;
}

function ChangeCCDate() {
    var _ccdate = 0;
    _ccdate = document.getElementById("ddlMonth").value.toString() + document.getElementById("ddlYear").value.toString();
    document.getElementById("ccdate").value = _ccdate.toString();
}

function restrictToNumbers(e) {
    if ([e.keyCode || e.which] == 8 || [e.keyCode || e.which] == 37 || [e.keyCode || e.which] == 9 || [e.keyCode || e.which] == 39) //this is to allow backspace
        return true;

    if ([e.keyCode || e.which] < 48 || [e.keyCode || e.which] > 57)
        e.preventDefault ? e.preventDefault() : e.returnValue = false;
}

function CheckCCDate() {
    var _status = true;
    try {
        var now = new Date();
        var nowY = now.getYear();
        var nowM = now.getMonth() + 1;
        if (nowY < 1900) { nowY += 1900; }
        var strccmonth = "";

        strccmonth = document.getElementById("ddlMonth").value.toString();
        var ccmonth = 0;
        if (strccmonth.indexOf("0") != -1) {
            ccmonth = parseInt(strccmonth.charAt(1));
        }
        else {
            ccmonth = parseInt(strccmonth);
        }

        var ccyear = parseInt("20" + document.getElementById("ddlYear").value.toString());
        if (nowY == ccyear) {
            if (nowM > ccmonth) {
                _status = false;
            }
        }
        else if (nowY > ccyear) {
            _status = false;
        }
    }
    catch (Ex) {
        _status = false;
    }
    return _status;
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function createCookie(name, value, mins) {
    if (mins) {
        var date = new Date();

        date.setTime(date.getTime() + (mins * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function CheckPage() {
    var _winloc = window.location.toString();
    //if (_winloc.indexOf("https://nera.i2pay.com.sg/etiqa/etiqa_payment.html") != -1) {

        var hcpa = querySt("hcpa");
        var hcrn = querySt("hcrn");
        var c = querySt("c");

        if (c != null && c == "1") {
            createCookie("_cx_hcpa", hcpa, 15);
            createCookie("_cx_hcrn", hcrn, 15);
            window.location = "etiqa_payment.html";
        }
        else {

            var cx_hcpa = readCookie("_cx_hcpa");

            var cx_hcrn = readCookie("_cx_hcrn");

            document.getElementById("lblAmount").innerHTML = "<strong>" + cx_hcpa.toString() + "</strong>";
            document.getElementById("amount").value = cx_hcpa;
            document.getElementById("ordernumber").value = cx_hcrn;

            window.onbeforeunload = function() {
                return "YOUR PERSONAL ACCIDENT INSURANCE'S PROPOSAL AND PAYMENT INFORMATION WOULD BE CANCELED.";
            }
        }
//    }
//    else {
//        window.location = "etiqa_payment.html";
//    }
}

CheckPage();
